package com.itheima.d6_thread_communication.demo;

import java.util.ArrayList;

 class Consumer extends Thread {
    private final ArrayList<String> plate;//盘子

    public Consumer(String name, ArrayList<String> plate) {
        super(name);
        this.plate = plate;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (plate) {
                while (plate.size() == 0) {
                    System.out.println("没包子了" + getName() + "休息会儿");
                    try {
                        plate.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                String baozi = plate.remove(0);
                System.out.println(getName() + "吃了一个" + baozi+",盘子中还有"+plate.size()+"个包子");
                plate.notifyAll();
            }

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
